/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto.web;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import proyecto.service.ProfesionService;
import proyecto.modelo.Requerimiento;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.ClaseFormacion;
import proyecto.modelo.Area;
import proyecto.modelo.NivelFormacion;
import proyecto.modelo.Profesion;
import proyecto.modelo.Empleado;

/** 
 *
 * @author Administrador
 */
public class ProfesionController extends MultiActionController {

    public ProfesionController() {
        //Initialize controller properties here or 
        //in the Web Application Context
        //setCommandClass(MyCommand.class);
        //setCommandName("MyCommandName");
        //setSuccessView("successView");
        //setFormView("formView");
    }

    //@Override
    protected void doSubmitAction(Object command) throws Exception {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    /*
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request,
    HttpServletResponse response,
    Object command,
    BindException errors) throws Exception {
    ModelAndView mv = new ModelAndView(getSuccessView());
    //Do something...
    return mv;
    }
     */

    private ProfesionService profesionService;

    public ProfesionService getProfesionService() {
        return profesionService;
    }

    public void setProfesionService(ProfesionService profesionService) {
        this.profesionService = profesionService;
    }

    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("/profesion_mantener");
    }

    

    public ModelAndView listar(HttpServletRequest request, HttpServletResponse response) {

        try {

            Collection<Profesion> profesiones = profesionService.listar();

            PrintWriter out = response.getWriter();
            //Construye una tabla con los datos de la lista
            String tabla = doLLenarProfesiones(profesiones);
            System.out.println(profesiones);
            out.println(tabla);
            out.close();

            request.setAttribute("profesiones", profesiones);
        } catch (IOException ex) {
            Logger.getLogger(CargoController.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (DAOExcepcion ex) {
            System.err.println(ex.toString());
        }
        return null;
    }

    private String doLLenarProfesiones(Collection<Profesion> profesiones) {

        int codigo = 0;
        String desc = "";

        String combo = "<select name=\"cboProfesion\" id=\"cboProfesion\">";

        for (Profesion a : profesiones) {
            codigo = a.getPro_Codigo();
            desc = a.getPro_Descripcion();
            combo = combo +
                    "<option value=\"" + codigo + "\">" + desc + "</option>";
        }

        combo = combo + "</select>";

        return combo;
    }

}
